﻿using UnityEngine;
using System.Collections;

public class ShowFPS : MonoBehaviour {

	/// <summary>
	/// 每次刷新计算的时间      帧/秒
	/// </summary>
	public float updateInterval = 0.5f;
	/// <summary>
	/// 最后间隔结束时间
	/// </summary>
	private double lastInterval;
	private int frames = 0;
	private float currFPS;
	private float minFPS = float.MaxValue;
	private float maxFPS = float.MinValue;

    string[] data = new string[4];

    // Use this for initialization
    void Start () {

		Application.targetFrameRate = -1;

		lastInterval = Time.realtimeSinceStartup;
		frames = 0;

        data[0] = SystemInfo.deviceName;
        data[1] = SystemInfo.operatingSystem;
        data[2] = SystemInfo.processorType;
        data[3] = SystemInfo.processorCount.ToString();
    }
	
	// Update is called once per frame
	void Update () {
		++frames;
		float timeNow = Time.realtimeSinceStartup;
		if (timeNow > lastInterval + updateInterval)
		{
			currFPS = (float)(frames / (timeNow - lastInterval));
			if(currFPS < minFPS)
				minFPS = currFPS;
			if(currFPS > maxFPS)
				maxFPS = currFPS;
			frames = 0;
			lastInterval = timeNow;
		}
	}
	
	private void OnGUI()
	{
		//		GUILayout.Box("FPS:" + currFPS.ToString("f2"),new GUIStyle.);

		//GUIStyle bb=new GUIStyle();
		//bb.normal.background = null;    //这是设置背景填充的
		//bb.normal.textColor=new Color(1,0,0);   //设置字体颜色的
		//bb.fontSize = 24;       //当然，这是字体颜色
		//GUI.Label(new Rect(100, 2, 50, 40), "FPS:" + currFPS.ToString("f2"),bb);

		//		GUILayout.Box("MinFPS:" + minFPS.ToString("f2"));
		//		GUILayout.Box("MaxFPS:" + maxFPS.ToString("f2"));
		//
		GUILayout.Box("-SystemInfo-");
		GUILayout.Box("Name:" + data[0]);
        //
        GUILayout.Box("System:" + data[1]);
        //
        GUILayout.Box("CPU:" + data[2]);
        GUILayout.Box("CPUCount:" + data[3]);
		//
		GUILayout.Box("FPS:" + currFPS.ToString("f2"));
	}
}
